<?php

namespace app\store\controller\group;

use app\store\controller\BaseController;
use app\store\model\GroupActivity;
use app\store\service\GroupActivityService;
use app\store\service\GroupGoodsService;
use think\exception\ValidateException;
use think\facade\View;

class ActivityController extends BaseController
{
    /**
     * 活动列表
     * */
    public function index()
    {   

        $param = $this->request->get();

        $groupActivityService = new GroupActivityService();
        $list = $groupActivityService->getGroupActivityList($param);

        return View::fetch('',['list' => $list]);
    }
    
    public function create()
    {

        return View::fetch();
    }


    /**
     * 活动选项
     * */
    public function option()
    {
        $param = $this->request->get();

        $groupActivityService = new GroupActivityService();
        $list = $groupActivityService->getGroupActivityOptions($param);

        View::engine()->layout(false);

        return View::fetch('',['list' => $list]);
    }
    

    /**
     *
     * */
    public function goods()
    {
        $param = $this->request->param();

        $groupGoodsService = new GroupGoodsService();

        $keyword = !empty($param['keyword']) ? $param['keyword'] : '';

        $list = $groupGoodsService->getActivityGoodsList($param['group_activity_id'],$keyword);

        View::engine()->layout(false);

        return View::fetch('',['group_activity_id' => $param['group_activity_id'],'list' => $list]);
    }

    /**
     * 删除活动商品
     * */
    public function deleteGoods()
    {
        $id = $this->request->param('id');

        $res = GroupActivityService::deleteGoods($id);

        return  $res ? json(['code' => 1,'msg' => '删除成功!']) : json(['code' => 0, 'msg' => '删除失败']);
    }


    public function edit()
    {   
        $id = $this->request->get('id/d');

        $groupActivityService = new GroupActivityService();
        $row = $groupActivityService->findGroupActivity($id);

        return View::fetch('',['row' => $row]);
    }

    public function save()
    {
        $param = $this->request->post();
        
        try{
            validate('GroupActivityValidate')->check($param);
        }catch (ValidateException $e){
            return json(['code' => 0, 'msg' => $e->getMessage()]);
        }

        $result = GroupActivityService::saveGroupActivity($param);

        return $result ? json(['code' => 1, 'msg' => '保存成功!']) : json(['code' => 0, 'msg' => '保存失败!']);
        
    }

    public function switch()
    {
        $param = $this->request->post();

        try {
            $this->validate($param,[
                'id' => 'require|integer',
                'status' => 'require|in:0,1',
            ]);
        }catch(ValidateException $e) {
            return json(['code' => 0, 'msg' => $e->getError()]);
        }
        
        GroupActivity::update($param);

        return json(['code' => 1, 'msg' => '更新成功!']);
    }

    public function update()
    {
        $param = $this->request->post();
        
        try{
            validate('GroupActivityValidate.edit')->check($param);
        }catch (ValidateException $e){
            return json(['code' => 0, 'msg' => $e->getMessage()]);
        }

        $result = GroupActivityService::updateGroupActivity($param);

        return $result ? json(['code' => 1, 'msg' => '更新成功!']) : json(['code' => 0, 'msg' => '更新失败!']);
    }


    public function delete()
    {
        $id = $this->request->get('id/d');
        
        $res = GroupActivity::destroy($id);

        return  $res ? json(['code' => 1,'msg' => '删除成功!']) : json(['code' => 0, 'msg' => '删除失败']);

    }
}